<?php
namespace jy_scrm;
defined('BY_JYA') or exit('error');
/*
	任务
*/
class task extends \plugin{
	/*
		跟进通知
	*/
	public function genjin(){
		$linelog=pdo_getall('jy_scrm_worker_linelog',array('ismsg'=>0,'nexttime <='=>TIMESTAMP,'status <>'=>3));
		if($linelog){
			pdo_update('jy_scrm_worker_linelog',array('ismsg'=>1),array('ismsg'=>0,'nexttime <='=>TIMESTAMP,'status <>'=>3));
			foreach($linelog as $lv){
				$worker=pdo_get('jy_scrm_worker',array('id'=>$lv['workerid']),'userid','userid');
				//if($userids){
				//	$userids=array_keys($userids);
				//	$touser=implode('|',$userids);
				//}
				$touser=$worker['userid'];
				$url=custom_url('jy_scrm/member.item//id='.$lv['userid'],true);
				$content="你有客户需要跟进！\n前往跟进，<a href=\"".$url."\">点击跟进</a>";
				if(!$touser){
					continue;
				}
				$post=array(
					'touser'=>$touser,
					'msgtype'=>'text',
					'text'=>array(
						'content'=>$content,
					),
				);
				model('core/wxwork_userapi')->message($post);
			}
		}
		return true;
	}
	/*
		跟进通知
	*/
	public function onegenjin($workerid,$content='',$url='',$isnoexi=false){
			if(!$workerid){
				if(!$isnoexi){
					exi('操作失败！员工ID不存在！','error');
				}
				return false;
			}
			$worker=pdo_get('jy_scrm_worker',array('id'=>$workerid),'userid','userid');
			$touser=$worker['userid'];
			$content =$content?:"你有客服需要跟进！\n前往跟进";
			if($url){
				$content .="<a href=\"".$url."\">点击跟进</a>";
			}
			if(!$touser){
				if(!$isnoexi){
					exi('操作失败！该员工没有同步企业微信的OPENID！','error');
				}
				return false;
			}
			$post=array(
					'touser'=>$touser,
					'msgtype'=>'text',
					'text'=>array(
						'content'=>$content,
					),
			);
			model('core/wxwork_userapi')->message($post);
			return true;
	}
	/*
		员工消息推送提醒
	*/
	public function workernotice(){
		$notice=pdo_getall('jy_scrm_member_notice',array('status'=>1));
		foreach($notice as $item){
			if(!$item['bumenid']){
				continue;
			}
			$item['timebox']=json_decode($item['timebox'],true);
			if($item['timebox']){
				$day=date('Y-m-d',TIMESTAMP);
				foreach($item['timebox'] as $tv){
					$time=$day.' '.$tv['time'];
					$time=strtotime($time);
					//小于任何一个时机段都推送
					if($item['sendtime']<$time){
						$ispass=true;
					}
				}
				if(!$ispass){
					continue;
				}
			}else{
				continue;
			}
			$workers=pdo_getall('jy_scrm_worker',array('status'=>1,'bumenids like'=>'%;'.$item['bumenid'].';%','uniacid'=>$item['uniacid']));
			foreach($workers as $wv){
				$content=$this->upcontent($item['content'],$wv);
				$res=$this->onegenjin($wv['id'],$content,'',true);
				if($res){
					$data=array(
						'uniacid'=>$item['uniacid'],
						'workerid'=>$wv['id'],
						'ismsg'=>1,
						'title'=>'日常推送消息',
						'content'=>$content,
						'createtime'=>TIMESTAMP,
					);
					pdo_insert('jy_scrm_worker_linelog',$data);
				}
			}
			$this->pdo_update('member_notice',array('sendtime'=>TIMESTAMP),array('id'=>$item['id']));
		}
	}
	/*
		整理跟进信息
	*/
	public function upcontent($content,$worker=array()){
		$content=json_decode($content,true);
		$msg='';
		$head=$content['head'];
		if($head){
			$datas=$this->gettpl_data($worker,0);
			$head=cfc('tools')->replaceTemplate($head,$datas);
			$msg .=$head."\n";
		}
		$buystatus0=$content['buystatus0'];
		if($buystatus0){
			$datas=$this->gettpl_data($worker,0);
			$buystatus0=cfc('tools')->replaceTemplate($buystatus0,$datas);
			$msg .=$buystatus0."\n";
		}
		$buystatus1=$content['buystatus1'];
		if($buystatus1){
			$datas=$this->gettpl_data($worker,1);
			$buystatus1=cfc('tools')->replaceTemplate($buystatus1,$datas);
			$msg .=$buystatus1."\n";
		}
		$buystatus2=$content['buystatus2'];
		if($buystatus2){
			$datas=$this->gettpl_data($worker,2);
			$buystatus2=cfc('tools')->replaceTemplate($buystatus2,$datas);
			$msg .=$buystatus2."\n";
		}
		$buystatus3=$content['buystatus3'];
		if($buystatus3){
			$datas=$this->gettpl_data($worker,3);
			$buystatus3=cfc('tools')->replaceTemplate($buystatus3,$datas);
			$msg .=$buystatus3."\n";
		}
		return $msg;
	}
	/*
		信息替换关键词
	*/
	public function gettpl_data($worker=array(),$status=0){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$link_time[0]=0;
		$link_time[1]=$daytime-1*24*3600;
		$link_time[3]=$daytime-3*24*3600;
		$sendmembernum=pdo_count('jy_scrm_member',array('buystatus >'=>0,'uniacid'=>$worker['uniacid'],'follow_user_userid'=>$worker['userid']));
		$sendupnum=pdo_count('jy_scrm_worker_linelog',array('workerid'=>$worker['id']));
		$nowstatusnum=pdo_count('jy_scrm_member',array('buystatus'=>$status,'uniacid'=>$worker['uniacid'],'follow_user_userid'=>$worker['userid']));
		$day1num=pdo_count('jy_scrm_member',array('uniacid'=>$worker['uniacid'],'follow_user_userid'=>$worker['openid'],'buystatus'=>$status,'link_time <='=>$link_time[1]));
		$day3num=pdo_count('jy_scrm_member',array('uniacid'=>$worker['uniacid'],'follow_user_userid'=>$worker['openid'],'buystatus'=>$status,'link_time <='=>$link_time[3]));
		$datas = array(
			array('name' => '员工姓名', 'value' => $worker['name']),
			array('name' => '已跟进人数', 'value' => $sendmembernum),
			array('name' => '已跟进条数', 'value' => $sendupnum),
			array('name' => '当前状态变化人数', 'value' => $nowstatusnum),
			array('name' => '当前阶段状态未改人数', 'value' => $nowstatusnum),
			array('name' => '超过1天数未跟进人数', 'value' => $day1num),
			array('name' => '超过3天数未跟进人数', 'value' => $day3num),
			array('name' => '发送时间点', 'value' => date('Y-m-d H:i:s',time())),
			array('name' => '可见员工', 'value' => $worker['name']),
			array('name' => '当前状态', 'value' => $this->user_buystatus[$status]),
		);
		return $datas;
	}
}